#include "frmkeyboard.h"
#include "ui_frmkeyboard.h"


frmKeyBoard::frmKeyBoard(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmKeyBoard)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus | Qt::X11BypassWindowManagerHint);
        this->setWindowModality(Qt::WindowModal);
        this->setAttribute(Qt::WA_DeleteOnClose);
    //setAttribute(Qt::WA_TranslucentBackground);//背景透明

        this->initFrm();
        //this->initStyleSheet();

        m_areaMovable = geometry();
        m_bPressed = false;

        setAreaMovable(ui->widget->contentsRect());

}

frmKeyBoard::~frmKeyBoard()
{
    delete ui;
}
void frmKeyBoard::initFocusWidget(QWidget* widget)
{
    m_focusWidget = widget;
}

void frmKeyBoard::initFrm()
{
    m_letterKeys.clear();
    m_NumberKeys.clear();
    QList<QPushButton*> pbtns = this->findChildren<QPushButton*>();
    foreach(QPushButton * pbtn, pbtns) {
        pbtn->setAutoRepeat(true);    //允许自动重复
        pbtn->setAutoRepeatDelay(500);//设置重复操作的时延
        if (pbtn->text() >= 'a' && pbtn->text() <= 'z') {
            connect(pbtn, &QPushButton::clicked, this, &frmKeyBoard::slotKeyLetterButtonClicked);
            m_letterKeys.push_back(pbtn);
        }
        else if (pbtn->text().toInt() > 0 && pbtn->text().toInt() <= 9 || pbtn->text() == "0") {
            connect(pbtn, &QPushButton::clicked, this, &frmKeyBoard::slotKeyNumberButtonClicked);
            m_NumberKeys.push_back(pbtn);
        }
        else{
            connect(pbtn, &QPushButton::clicked, this, &frmKeyBoard::slotKeyButtonClicked);
        }
    }
}

void frmKeyBoard::initStyleSheet()
{
    QString qss;
    //can not have style on raspberry
/*
    qss += "#pushButton_closeKeyboard{background-color:rgba(0,0,0,0); border:0px}";
    qss += "#pushButton_closeKeyboard:hover{background-color:#b30220;}";
*/
    qss += "QHeaderView::section {"
                   "background-color: black;"
                   "border: 1px solid gray;"
                   "height: 5px; /* 调整高度 */"
                   "}";
    this->setStyleSheet(qss);
}

void frmKeyBoard::slotKeyButtonClicked()
{
    QPushButton* pbtn = (QPushButton*)sender();
    QString objectName = pbtn->objectName();
    if (objectName == "pushButton_closeKeyboard") {
           emit hiding();
    }
    else if (pbtn->text() == "Backspace") {
        QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
        QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Backspace, Qt::NoModifier);
        QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
        QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
    }
    else if (pbtn->text() == "Caps") {
        if (ui->pushButton_caps->isChecked()) {
            ui->pushButton_caps->setChecked(true);
            for (auto pbtnKey : m_letterKeys) {
                pbtnKey->setText(pbtnKey->text().toUpper());
            }
        }
        else {
                ui->pushButton_caps->setChecked(false);
                for (auto pbtnKey : m_letterKeys) {
                    pbtnKey->setText(pbtnKey->text().toLower());
             }
        }
    }
    else if(pbtn->text() == "Space") {
        QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, " ");
        QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Space, Qt::NoModifier, " ");
        QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
        QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
    }
    else {
            QKeyEvent keyPress(QEvent::KeyPress, m_mapSymbolKeys.value(pbtn->text()), Qt::NoModifier, pbtn->text());
            QKeyEvent keyRelease(QEvent::KeyRelease, m_mapSymbolKeys.value(pbtn->text()), Qt::NoModifier, pbtn->text());
            QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
            QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
        }

}

void frmKeyBoard::slotKeyLetterButtonClicked()
{
    QPushButton* pbtn = (QPushButton*)sender();
    if (pbtn->text() >= 'a' && pbtn->text() <= 'z') {
        QKeyEvent keyPress(QEvent::KeyPress, int(pbtn->text().at(0).toLatin1()) - 32, Qt::NoModifier, pbtn->text());
        QKeyEvent keyRelease(QEvent::KeyRelease, int(pbtn->text().at(0).toLatin1()) - 32, Qt::NoModifier, pbtn->text());
        QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
        QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
    }
    else if (pbtn->text() >= 'A' && pbtn->text() <= 'Z') {
        QKeyEvent keyPress(QEvent::KeyPress, int(pbtn->text().at(0).toLatin1()), Qt::NoModifier, pbtn->text());
        QKeyEvent keyRelease(QEvent::KeyRelease, int(pbtn->text().at(0).toLatin1()), Qt::NoModifier, pbtn->text());
        QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
        QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
    }
}

void frmKeyBoard::slotKeyNumberButtonClicked()
{
    QPushButton* pbtn = (QPushButton*)sender();
    QKeyEvent keyPress(QEvent::KeyPress, pbtn->text().toInt() + 48, Qt::NoModifier, pbtn->text());
    QKeyEvent keyRelease(QEvent::KeyRelease, pbtn->text().toInt() + 48, Qt::NoModifier, pbtn->text());
    QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
    QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}


void frmKeyBoard::mousePressEvent(QMouseEvent *e)
{
  //鼠标左键
  if(e->button() == Qt::LeftButton)
  {
  //这里要用m_areaMovable.contains(m_ptPress);来判断该点是否被包含在可移动区域中，由于被判断的区域是相对坐标，所以这里也必须用e->pos获取相对坐标
  m_ptPress = e->pos();
  qDebug() << pos() << e->pos() << m_ptPress;
  m_bPressed = m_areaMovable.contains(m_ptPress);
  }
}


void frmKeyBoard::mouseMoveEvent(QMouseEvent *e)
{
  if(m_bPressed)
  {
  qDebug() << pos() << e->pos() << m_ptPress;
  //用拖动到的位置的全局坐标减去获取的初始左键的相对坐标(算了我不理解，瞎整居然整出来防抖了)
  move(e->globalPos() - m_ptPress);
  }
}


void frmKeyBoard::mouseReleaseEvent(QMouseEvent *)
{
  m_bPressed = false;
}

//设置鼠标按下的区域
void frmKeyBoard::setAreaMovable(const QRect rt)
{
  if(m_areaMovable != rt)
  {
  m_areaMovable = rt;
  }
}


